package org.jradweb.core.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class JHttpServletRequestWrapper extends HttpServletRequestWrapper {
	private HttpServletRequest request;

	public JHttpServletRequestWrapper(HttpServletRequest request) {
		super(request);
		this.request = request;
	}

	public String getParameter(String name) {
		String value = request.getParameter(name);
		if (value != null) {
			return value;
		}

		Map<String, String> uriParams = this.getUriParameters(request);
		return uriParams == null ? null : uriParams.get(name);
	}

	public Map getParameterMap() {
		Map mutableMap=new HashMap();
		Map map = request.getParameterMap();
		Map<String, String> uriParams = this.getUriParameters(request);
		if (uriParams != null) {
			for (String key : uriParams.keySet()) {
				mutableMap.put(key, new String[] { uriParams.get(key) });
			}
		}
		mutableMap.putAll(map);
		return mutableMap;
	}

	public Enumeration getParameterNames() {
		Enumeration en = request.getParameterNames();
		// TODO add to enumeration
		return en;
	}

	public String[] getParameterValues(String name) {
		return request.getParameterValues(name);
	}

	private Map<String, String> getUriParameters(HttpServletRequest request) {
		Map<String, String> parameters = (Map<String, String>) request
				.getAttribute(CommonAttributes.REQUEST_URL_PARAMETERS);
		return parameters;
	}

}
